package wei.qiang.ye.springbootcache.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import wei.qiang.ye.springbootcache.entity.UserDO;

@Repository
@CacheConfig(cacheNames = "users") //统一配置该 UserMapper 使用的缓存名为 users
public interface UserMapper extends BaseMapper<UserDO> {

    @Cacheable(key = "#a0")
    default UserDO searchById(Integer id) {
        return this.selectById(id);
    }

    @CachePut(key = "#user.id")
    default UserDO insert0(UserDO user) {
        // 插入记录
        this.insert(user);
        // 返回用户
        return user;
    }

    @CacheEvict(key = "#a0")
    int deleteById(Integer id);
}
